import httpx

import config


async def send_gotify(
        app_token: str,
        message: str,
        title: str,
        priority: int = 5,
        server_url: str = config.GOTIFY_URL
) -> bool:
    """
    异步发送消息到 Gotify 服务器（默认地址无需传参）
    :param app_token: APPS 页面生成的 token（必填）
    :param message: 消息正文（必填）
    :param title: 消息标题（默认'通知'）
    :param priority: 优先级 0-10（默认5）
    :param server_url: 默认 Gotify 服务地址（如无需修改可省略）
    :return: 是否发送成功
    """
    url = f"{server_url}/message?token={app_token}"

    try:
        async with httpx.AsyncClient(timeout=10) as client:
            response = await client.post(
                url,
                data={"title": title, "message": message, "priority": priority}
            )
            response.raise_for_status()
            return True
    except (httpx.NetworkError, httpx.HTTPStatusError) as e:
        print(f"发送失败: {str(e)}")
        return False